<?php
	class Articles extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->model(array('article', 'komentar', 'photo'));
			$this->load->helper(array('form', 'dharma_date_helper'));
			$this->userdata = $this->session->userdata('userdata');
			if($this->userdata){
				$recent_articles = $this->article->get_article(NULL, NULL, 5);
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6);
			}else{
				$recent_articles = $this->article->get_article(NULL, NULL, 5, 0, 'public');
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6, 0, 'public');
			}
			$this->right_sidebar = array('categories' => unserialize(ARTICLE_CATEGORY),
										 'recent_articles' => $recent_articles,
										 'recent_photos' => $recent_photos);
		}
		
		public function index(){
			if($this->userdata){
				$data['articles'] = $this->article->get_article();
			}else{
				$data['articles'] = $this->article->get_article(NULL, NULL, NULL, NULL, 'public');
			}
			if($data['articles']){
				foreach($data['articles'] as $article){
					$com[] = $this->komentar->get_current_komentar('artikel', $article['id_artikel'], 'LIST_KOMENTAR');
				}
			}else{
				$com = NULL;
			}
			$data['total_comments'] = $com;
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/index', $data);
		}
		
		public function article_category($category){
			if($this->userdata){
				$data['articles'] = $this->article->get_article_order_by_category($category);
			}else{
				$data['articles'] = $this->article->get_article_order_by_category($category, 'public');
			}
			if($data['articles']){
				foreach($data['articles'] as $article){
					$com[] = $this->komentar->get_current_komentar('artikel', $article['id_artikel'], 'LIST_KOMENTAR');
				}
				$data['total_comments'] = $com;
			}else{
				$data['total_comments'] = NULL;
			}
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/index', $data);
		}
		
		public function detail($id){
			$article = $this->article->get_article($id);
			$data = array('article' => $article[0],
					      'komentars' => $this->komentar->get_current_komentar('artikel', $id),
					      'user' => $this->userdata);
			if($this->userdata){
				if(isset($_POST['submit_komentar'])){
					$post_data = array('jenis' => 'artikel',
									   'id_item' => $id,
									   'isi' => $this->input->post('komentar'),					
									   'id_user' => $this->userdata['id_user']
									   );
					$this->komentar->add($post_data);
					redirect(site_url('/articles/detail/'.$id));
				}
			}
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/detail', $data);
		}
		
		public function user_articles(){
			$data['articles'] = $this->article->get_article(NULL, $this->userdata['id_user']);
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/user_article', $data);
		}
		
		public function add(){
			if(isset($_POST['article_submit'])){
				$random           = rand(000000,999999);
				$image_upload 	  = $_FILES['image_article']['name'];
				if (empty($image_upload)) {
					copy("./images/article_images/default.jpg", "./images/article_images/" . $random . "_default.jpg");
					$article_image_name = $random."_default.jpg";
				}else{
					$article_image_name = $random."_".strtolower($image_upload);
				}
				$post_data = array(
							'id_user' => $this->userdata['id_user'],
							'judul' => $this->input->post('judul'),
							'kategori' => $this->input->post('kategori'),
							'isi' => $this->input->post('isi'),
							'status' => $this->input->post('status'),
							'foto' => $article_image_name
							);
				$id = $this->article->add($post_data);
				//upload
				$config['upload_path'] = './images/article_images/';
				$config['allowed_types'] = 'jpg|png|gif|jpeg';
				$config['file_name'] = $article_image_name;
				$this->load->library('upload', $config);
				if (!$this->upload->do_upload('image_article', TRUE)) { // upload image to server
					$error = $this->upload->display_errors();
					?><script type="text/javascript"> alert('<?php echo $error; ?>'); </script><?php
					$this->article->delete($id);
				} else {
					redirect(site_url('/articles/user_articles'));
				}
			}
			$data['categories'] = unserialize(ARTICLE_CATEGORY);
			$data['articles'] = NULL;
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/form', $data);
		}
		
		public function edit($id){
			$article = $this->article->get_article($id);
			if(isset($_POST['article_submit'])){
				$random           = rand(000000,999999);
				$image_upload 	  = $_FILES['image_article']['name'];
				if ($image_upload != null) {
					$config['upload_path'] = './images/article_images/';
					$config['allowed_types'] = 'jpg|png|gif|jpeg';
					$url = './images/article_images/'.$article[0]['foto'];
					if (isset($url)) {
						if($remove = unlink($url)){
							$config['file_name'] = $random."_".$image_upload;
							$this->load->library('upload', $config);
							if (!$this->upload->do_upload('image_article', TRUE)) { // upload image to server
								$error = $this->upload->display_errors();
								echo $error;
							} 
						}else{
							echo "not removed"; die;
						}
					}
					$post_data = array(
									'judul' => $this->input->post('judul'),
									'kategori' => $this->input->post('kategori'),
									'isi' => $this->input->post('isi'),
									'status' => $this->input->post('status'),
									'foto'	=> $random."_".strtolower($image_upload)
									);
					
				}else{
					$post_data = array(
									'judul' => $this->input->post('judul'),
									'kategori' => $this->input->post('kategori'),
									'isi' => $this->input->post('isi'),
									'status' => $this->input->post('status')
									);
				}
				$this->article->edit($id, $post_data);
				redirect(site_url('articles/user_articles'));
			}
			$data['categories'] = unserialize(ARTICLE_CATEGORY);
			$data['articles'] = $article;
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('article/form', $data);
		}
		
		public function delete($id){
			$article = $this->article->get_article($id);
			$this->article->delete($id);
			$this->komentar->delete($id);
			unlink('./images/article_images/'.$article[0]['foto']);
			redirect('articles/user_articles');
		}
	}
?>